//
//  TravelCommunityViewController.m
//  W.E.S.T
//
//  Created by xalo on 16/7/14.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "TravelCommunityViewController.h"

@interface TravelCommunityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *titleArray;
@property (nonatomic,strong)NSMutableArray *collectionArray0;
@property (nonatomic,strong)NSMutableArray *collectionArray1;
@property (nonatomic,strong)NSMutableArray *collectionArray2;
@property (nonatomic,strong)NSMutableArray *collectionArray3;
@property (nonatomic,strong)NSMutableArray *collectionArray4;
@property (nonatomic,strong)NSMutableArray *collectionArray5;
@property (nonatomic,strong)NSMutableArray *collectionArray6;
@property (nonatomic,strong)NSMutableArray *collectionArray7;
@property (nonatomic ,strong)NSMutableArray *allCityDataArray;


@end

@implementation TravelCommunityViewController

-(NSMutableArray *)allCityDataArray{
    if (!_allCityDataArray) {
        _allCityDataArray = [NSMutableArray array];
    }
    return _allCityDataArray;
}
- (NSMutableArray *)titleArray{
    if (!_titleArray) {
        _titleArray=[NSMutableArray array];
    }
    return _titleArray;
}
- (NSMutableArray *)collectionArray0{
    if (!_collectionArray0) {
        _collectionArray0=[NSMutableArray array];
    }
    return _collectionArray0;
}
- (NSMutableArray *)collectionArray1{
    if (!_collectionArray1) {
        _collectionArray1=[NSMutableArray array];
    }
    return _collectionArray1;
}
- (NSMutableArray *)collectionArray2{
    if (!_collectionArray2) {
        _collectionArray2=[NSMutableArray array];
    }
    return _collectionArray2;
}
- (NSMutableArray *)collectionArray3{
    if (!_collectionArray3) {
        _collectionArray3=[NSMutableArray array];
    }
    return _collectionArray3;
}
- (NSMutableArray *)collectionArray4{
    if (!_collectionArray4) {
        _collectionArray4=[NSMutableArray array];
    }
    return _collectionArray4;
}
- (NSMutableArray *)collectionArray5{
    if (!_collectionArray5) {
        _collectionArray5=[NSMutableArray array];
    }
    return _collectionArray5;
}
- (NSMutableArray *)collectionArray6{
    if (!_collectionArray6) {
        _collectionArray6=[NSMutableArray array];
    }
    return _collectionArray6;
}
- (NSMutableArray *)collectionArra7{
    if (!_collectionArray7) {
        _collectionArray7=[NSMutableArray array];
    }
    return _collectionArray7;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize=CGSizeMake(150, 50);
        layout.sectionInset=UIEdgeInsetsMake(20, 10, 20, 10);
        //区头大小
        layout.headerReferenceSize=CGSizeMake(kWidth,50);
        _collectionView=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        
        _collectionView.backgroundColor=[UIColor whiteColor];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"旅游社区";
    
    [self.view addSubview:self.collectionView];
    //注册
    [self.collectionView registerNib:[UINib nibWithNibName:@"TravelCommunityCell" bundle:nil] forCellWithReuseIdentifier:@"TravelCommunityModel_cell"];
    //特别注意:使用collectionView的头部view时,先注册类型[头还是脚]
    // 注册区头
    [self.collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [RequestManager fetchDataWithUrl:kTravelCommunityUrl parameter:@{} method:@"GET" compeleted:^(NSData *data) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    //[dic writeToFile:@"/Users/xalo/Desktop/data.plist" atomically:YES];
        for (NSDictionary *titleDic in dic[@"data"]) {
            [self.titleArray addObject:titleDic[@"name"]];
        }

             NSMutableArray *arr0=[NSMutableArray arrayWithArray:@[dic[@"data"][0]]];
            for (NSDictionary *dictt in arr0) {
                
                for (NSDictionary *smallDic in dictt[@"group"]) {
                TravelCommunityModel *model=[[TravelCommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.collectionArray0 addObject:model];
                
                }
        }
        NSLog(@"-+---+-+-+-+-+-+-+-+-+%ld",self.collectionArray0.count);
        NSMutableArray *arr1=[NSMutableArray arrayWithArray:@[dic[@"data"][1]]];
        for (NSDictionary *dictt in arr1) {
            
            for (NSDictionary *smallDic in dictt[@"group"]) {
                TravelCommunityModel *model=[[TravelCommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.collectionArray1 addObject:model];
                
            }
        }
        NSLog(@"-+---+-+-+-+-+-+-+-+-+%ld",self.collectionArray1.count);
        NSMutableArray *arr2=[NSMutableArray arrayWithArray:@[dic[@"data"][2]]];
        for (NSDictionary *dictt in arr2) {
            
            for (NSDictionary *smallDic in dictt[@"group"]) {
                TravelCommunityModel *model=[[TravelCommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.collectionArray2 addObject:model];
                
            }
        }
        NSLog(@"-+---+-+-+-+-+-+-+-+-+%ld",self.collectionArray2.count);
        NSMutableArray *arr3=[NSMutableArray arrayWithArray:@[dic[@"data"][3]]];
        for (NSDictionary *dictt in arr3) {
            
            for (NSDictionary *smallDic in dictt[@"group"]) {
                TravelCommunityModel *model=[[TravelCommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.collectionArray3 addObject:model];
                
            }
        }
        NSLog(@"-+---+-+-+-+-+-+-+-+-+%ld",self.collectionArray3.count);
        NSMutableArray *arr4=[NSMutableArray arrayWithArray:@[dic[@"data"][4]]];
        for (NSDictionary *dictt in arr4) {
            
            for (NSDictionary *smallDic in dictt[@"group"]) {
                TravelCommunityModel *model=[[TravelCommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.collectionArray4 addObject:model];
                
            }
        }
        NSLog(@"-+---+-+-+-+-+-+-+-+-+%ld",self.collectionArray4.count);
        NSMutableArray *arr5=[NSMutableArray arrayWithArray:@[dic[@"data"][5]]];
        for (NSDictionary *dictt in arr5) {
            
            for (NSDictionary *smallDic in dictt[@"group"]) {
                TravelCommunityModel *model=[[TravelCommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.collectionArray5 addObject:model];
                
            }
        }
        NSLog(@"-+---+-+-+-+-+-+-+-+-+%ld",self.collectionArray5.count);
        NSMutableArray *arr6=[NSMutableArray arrayWithArray:@[dic[@"data"][6]]];
        for (NSDictionary *dictt in arr6) {
            
            for (NSDictionary *smallDic in dictt[@"group"]) {
                TravelCommunityModel *model=[[TravelCommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.collectionArray6 addObject:model];
               
            }
        }
         NSLog(@"-+---+-+-+-+-+-+-+-+-+%ld",self.collectionArray6.count);
        NSMutableArray *arr7=[NSMutableArray arrayWithArray:@[dic[@"data"][7]]];
        for (NSDictionary *dictt in arr7) {
            
            for (NSDictionary *smallDic in dictt[@"group"]) {
                TravelCommunityModel *model=[[TravelCommunityModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.collectionArray7 addObject:model];
                
            }
            
        }
        NSLog(@"-+---+-+-+-+-+-+-+-+-+%ld",self.collectionArray7.count);
       //将各个小分区存放在大数组中,形成分组模型,为了方便cell取值
        self.allCityDataArray =[NSMutableArray arrayWithObjects:self.collectionArray0,self.collectionArray1,self.collectionArray2,self.collectionArray3,self.collectionArray4,self.collectionArray5,self.collectionArray6,self.collectionArray7, nil];

        
        
    } updateUI:^{
        
        [self.collectionView reloadData];
    }];
    
}

#pragma mark-----------------UICollectionView的协议------------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    ;
    if (section == 0) {
        return self.collectionArray0.count;
    }else if (section == 1){
        return self.collectionArray1.count;
    }else if (section == 2){
        return self.collectionArray2.count;
    }else if (section == 3){
        return self.collectionArray3.count;
    }else if (section == 4){
        return self.collectionArray4.count;
    }else if (section == 5){
        return self.collectionArray5.count;
    }else if (section == 6){
        return self.collectionArray6.count;
    }else if (section == 7){
        return self.collectionArray7.count;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TravelCommunityCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"TravelCommunityModel_cell" forIndexPath:indexPath];
    

    [cell setCellWithModel:self.allCityDataArray[indexPath.section][indexPath.row]];

    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSLog(@"***********************%ld",self.titleArray.count);
    return self.titleArray.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind==UICollectionElementKindSectionHeader) {
        HeaderView *headerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
       //设置区头的内容
        for (int i = 0; i < self.titleArray.count; i++) {
            
            if (indexPath.section==i) {
                
                headerView.label.text=self.titleArray[i];
            }
        }
       
        
     
       return headerView;
        
    }
    return nil;
}

//- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
//    UILabel *label=[[UILabel alloc]init];
//    label.text=self.titleArray[indexPath.row];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
